package com.freak.wanandroid.model.search;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.callback.ApiCallback;
import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.wanandroid.bean.HistoryData;
import com.freak.wanandroid.bean.TopSearchData;

import java.util.List;

/**
 * Created by 74099 on 2018/9/5.
 */

public class SearchViewModel extends BaseViewModel<SearchRepository> {
    protected MutableLiveData<List<TopSearchData>> mGetTopSearchData = new MutableLiveData<>();
    protected MutableLiveData<String> mGetTopSearchDataError = new MutableLiveData<>();
    protected MutableLiveData<String> mJumpActivity = new MutableLiveData<>();

    public SearchViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * Load all history data
     *
     * @return all history data
     */
    List<HistoryData> loadAllHistoryData() {
        return mRepository.loadAllHistoryData();
    }

    /**
     * Add history data
     *
     * @param data history data
     */
    void addHistoryData(String data) {
        mRepository.addHistoryData(data);
        mJumpActivity.postValue(data);
    }

    /**
     * 热搜
     */
    void getTopSearchData() {
        mRepository.getTopSearchData(new SubscriberCallBack<>(new ApiCallback<List<TopSearchData>>() {
            @Override
            public void onSuccess(List<TopSearchData> model) {
                mGetTopSearchData.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetTopSearchDataError.postValue(msg);
            }
        }));
    }

    /**
     * Clear history data
     */
    void clearHistoryData() {
        mRepository.clearHistoryData();
    }
}
